<template>
  <div class="examinationsWrap">

  <div class="pageTableInner">
    <div class="detailHeader">
      <span style="text-indent: 5px;letter-spacing:3px;">{{examineTask.examineName}}</span>
      <div class="iconWrap">
        <router-link to="/examinationResults"><i class="iconfont icon-fanhui"></i></router-link>
      </div>
    </div>
    <div class="tableContainer" style="top:35px;border-radius: 0;">
      <div class="tableInner" ref="tableInner">
        <el-table
          ref="multipleTable"
          :data="rows"
          stripe
          show-summary
          :summary-method="getSummaries"
          :height="tableHeight"
          :span-method="objectSpanMethod"
          tooltip-effect="dark"
          style="width: 100%"
        >
          <el-table-column
            label="指标类别"
            prop="classifyName"
            show-overflow-tooltip
          >
          </el-table-column>
          <el-table-column
            label="类别分值"
            prop="classifyScore"
            show-overflow-tooltip
          >
          </el-table-column>
          <el-table-column
            prop="itemsName"
            label="指标项"
            show-overflow-tooltip
          >
          </el-table-column>
          <el-table-column
            prop="standardScore"
            label="指标分值"
            show-overflow-tooltip
          >
          </el-table-column>

          <el-table-column
            v-for="(item,key) in tableCompanyList"
            :key="key"
            :label="item.enterpriseName"
            :prop="'value'+key"
            show-overflow-tooltip>
            <template slot-scope="scope">
              <div style="cursor: pointer;"  @click="$refs.detailDialog.dialogOpen({enterpriseNo:scope.row.enterpriseTotalList[key].enterpriseNo,itemsId:scope.row.itemsId,templateId:scope.row.enterpriseTotalList[key].templateId,taskId:scope.row.enterpriseTotalList[key].taskId,totalScore:scope.row.enterpriseTotalList[key].totalScore})">{{scope.row.enterpriseTotalList[key].totalScore}}</div>
            </template>
          </el-table-column>
        </el-table>
        <div v-if="$store.state.userInfo.roleLevel==1" style="float: right;margin-top: 20px;">
          <el-button type="primary" size="mini" @click="confirmExamine(0)">不同意本次考核结果</el-button>
          <el-button type="primary" size="mini" @click="confirmExamine(1)">同意本次考核结果</el-button>
        </div>
      </div>
    </div>
  </div>
    <detailDialog ref="detailDialog"></detailDialog>
    <el-dialog :close-on-click-modal="false"
      title="不同意本次考核意见"
      :append-to-body="true"
      :visible.sync="dialogVisible"
      :before-close="handleClose">
      <el-input
        type="textarea"
        :rows="3"
        :maxlength="100"
        placeholder="请输入考核意见"
        v-model="examineOpinion">
      </el-input>

      <span slot="footer"  class="dialog-footer">
    <el-button @click="dialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="confirmExamineReult(0)">确 定</el-button>
  </span>
    </el-dialog>
  </div>
</template>

<script>
  import detailDialog from './detailDialog.vue';
  export default {
    props: ["examineName","examineTask"],
    data(){
      return {
        rows: [],
        multipleSelection: [],
        tableHeight: 400,
        spanArr: [],
        tableCompanyList: [],
        taskId:1,
        dialogVisible:false,
        examineOpinion:""
      }
    },
    components: {detailDialog},
    created(){
      if(this.examineTask.id){
        this.getExamineInfo(this.examineTask.templateId,this.examineTask.id)
      }
    },
    mounted(){
      this.$nextTick(()=>{
        //表格滚动
        this.tableHeight=document.querySelector(".examinationsWrap").clientHeight-120
      })
    },

    methods: {
      handleClose(){
        this.examineOpinion=""
        this.dialogVisible = false
      },
      confirmExamine(hasAgree){
        if(hasAgree){ //同意
          this.$confirm('确认同意本次考核结果？', '确认信息', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            this.confirmExamineReult(hasAgree)
          }).catch(() => {

          });
        }else {
          this.dialogVisible = true
        }

      },
      confirmExamineReult(hasAgree){
        if(hasAgree==0&&this.examineOpinion==""){
          this.$message({
            type: 'warning',
            message: "请输入考核意见"
          });
          return
        }
        this.axios.post(process.env.API_HOST + `/examine/confirmExamineReult`, {
          hasAgree: hasAgree,
          taskId: this.examineTask.id,
          agencyId: this.$store.state.agencyId,
          examineOpinion:this.examineOpinion
        })
          .then(response => {
            let result = response.data
            this.dialogVisible = false
            if (result.code == 200) {
              this.$message({
                type: 'success',
                message: '操作成功!'
              });

            }
          })
      },
      //获取所有信息
      getExamineInfo(templateId, taskId){
        this.axios.post(process.env.API_HOST + `/examine/selectEnterpriseTotalList`, {
          templateId: templateId,
          taskId: taskId
        })
          .then(response => {
            let result = response.data
            if (result.code == 200) {

              this.rows = []
              result.result.forEach(item => {
                item.itemList.forEach(item1 => {
                  this.rows.push({
                    ...item1,
                    classifyId: item.classifyId,
                    classifyName: item.classifyName,
                    classifyScore: ""
                  })
                })

              })
              this.tableCompanyList=[]
              if (this.rows.length > 0) {
                this.rows[0].enterpriseTotalList.forEach((item,key)=>{
                  this.$store.state.companyInfos.forEach(item1=>{
                    if(item.enterpriseNo==item1.enterpriseNo){

                      this.tableCompanyList.push({enterpriseName:item1.enterpriseName,totalScore:item.totalScore})
                    }
                  })
                })
                this.spanArr = this.getSpanArr(this.rows, "classifyName")
                this.rows.forEach((item, key) => {
                  this.tableCompanyList.forEach((item1,index)=>{
                    item['value'+index]=item.enterpriseTotalList[index].totalScore
//                    item['value'+index]=item1.totalScore
                  })
                  this.totalScore += item.standardScore
                  this.getclassifyScore(key)
                })
              }

            }
          })

      },
      objectSpanMethod({row, column, rowIndex, columnIndex}) {
        if (columnIndex === 0 || columnIndex === 1) {
          const _row = this.spanArr[rowIndex];
          const _col = _row > 0 ? 1 : 0;
          return {
            rowspan: _row,
            colspan: _col
          };
        }
      },
      getclassifyScore(index){
        let sub = 0, isPass = true, classifyScore = 0
        this.spanArr.forEach(item => {
          sub += item
          if (isPass && sub > index) {
            isPass = false
            const sliceArr = this.rows.slice(sub - item, sub)
            sliceArr.forEach(item => {
              classifyScore += this.toDecimal(item.standardScore)
            })
            sliceArr.forEach(item => {
              item.classifyScore = classifyScore
            })
          }
        })

      },
      getSummaries(param) {
        let _this=this
        const {columns, data} = param;
        const sums = [];
        columns.forEach((column, index) => {
          if (index === 0) {
            sums[index] = '';
            return;
          } else if (index === 1) {
            sums[index] = '考核成绩';
            return;
          }
          const values = data.map(item => Number(item[column.property]));
          if (!values.every(value => isNaN(value))) {
            sums[index] = values.reduce((prev, curr) => {
              const value = Number(curr);
              if (!isNaN(value)) {
                return _this.toDecimal(prev + curr);
              } else {
                return prev;
              }
            }, 0);
            sums[index] += " 分"
          } else {
//            sums[index] = 'N/A';
            sums[index] = '';
          }
        });

        return sums;
      },

    },
  }
</script>

<style lang="stylus" rel="stylesheet/stylus">
  @import "../../../common/stylus/mixin.styl"
  .examinationsWrap
    page1()


</style>
